﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NewDevice.RS232
{
    /// <summary>
    /// 程控电源
    /// </summary>
    public class IT6302 : OldSerialBase
    {
        private readonly int _cmdRespTime = 200;

        /// <summary>
        /// 构造函数传入设备名，一台设备链接多台电源时区分名称
        /// </summary>
        /// <param name="devName"></param>
        public IT6302(string devName = "IT6302")
        {
            deviceName = devName;
        }

        /// <summary>
        /// 设置电源输出状态
        /// </summary>
        /// <param name="status"></param>
        public void SetOutputStatus(bool status)
        {
            string strStatus = status ? "1" : "0";
            SendAsciiString($"OUTP {strStatus}\r\n");
            Thread.Sleep(_cmdRespTime);
        }

        /// <summary>
        /// 设置电源指定通道状态
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="status"></param>
        public void SetOutputStatus(int channel, bool status)
        {
            SetChannel(channel);
            string strStatus = status ? "1" : "0";
            SendAsciiString($"SOUR:CHAN:OUTP {strStatus}\r\n");
            Thread.Sleep(_cmdRespTime);
        }

        /// <summary>
        /// 读取电源输出状态
        /// </summary>
        /// <returns></returns>
        public bool GetOutputStatus()
        {
            var ret = SendAsciiStringAndWaitResponse("OUTPut:STATe?\r\n");
            return ret == "1";
        }

        /// <summary>
        /// 读取当前通道电源输出状态
        /// </summary>
        /// <returns></returns>
        public bool GetOutputStatus(int channel)
        {
            SetChannel(channel);
            var ret = SendAsciiStringAndWaitResponse("SOUR:CHAN:OUTP?\r\n");
            return ret == "1";
        }

        /// <summary>
        /// 设置指定通道电压
        /// </summary>
        /// <param name="channel">1-3</param>
        /// <param name="voltage"></param>
        public void SetVoltage(int channel, double voltage)
        {
            SetChannel(channel);
            SendAsciiString($"SOUR:VOLT {voltage}\r\n");
            Thread.Sleep(_cmdRespTime);
        }

        /// <summary>
        /// 同时设置三通道电压值
        /// </summary>
        /// <param name="v1"></param>
        /// <param name="v2"></param>
        /// <param name="v3"></param>

        public void SetVoltage(double v1, double v2, double v3)
        {
            SendAsciiString($"APP:VOLT {v1},{v2},{v3}\r\n");
            Thread.Sleep(_cmdRespTime);
        }

        /// <summary>
        /// 设置指定通道电流
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="current"></param>
        public void SetCurrent(int channel, double current)
        {
            SetChannel(channel);
            SendAsciiString($"SOUR:CURR {current}\r\n");
            Thread.Sleep(_cmdRespTime);
        }

        /// <summary>
        /// 同时设置三通道电流值
        /// </summary>
        /// <param name="v1"></param>
        /// <param name="v2"></param>
        /// <param name="v3"></param>
        public void SetCurrent(double v1, double v2, double v3)
        {
            SendAsciiString($"APP:CURR {v1},{v2},{v3}\r\n");
            Thread.Sleep(_cmdRespTime);
        }

        /// <summary>
        /// 读取设定电压
        /// </summary>
        /// <param name="channel"></param>
        /// <returns></returns>
        public double GetVoltage(int channel)
        {
            SetChannel(channel);
            var ret = SendAsciiStringAndWaitResponse("SOUR:VOLT?\r\n");
            return double.Parse(ret);
        }

        /// <summary>
        /// 读取设定电流
        /// </summary>
        /// <param name="channel"></param>
        /// <returns></returns>
        public double GetCurrent(int channel)
        {
            SetChannel(channel);
            var ret = SendAsciiStringAndWaitResponse("SOUR:CURR?\r\n");
            return double.Parse(ret);
        }

        /// <summary>
        /// 测量出电压
        /// </summary>
        /// <param name="channel"></param>
        public double MeasVoltage(int channel)
        {
            SetChannel(channel);
            var ret = SendAsciiStringAndWaitResponse("MEAS:VOLT?\r\n");
            return double.Parse(ret);
        }

        /// <summary>
        /// 测量输出电流
        /// </summary>
        /// <param name="channel"></param>
        /// <returns></returns>
        public double MeasCurrent(int channel)
        {
            SetChannel(channel);
            var ret = SendAsciiStringAndWaitResponse("MEAS:CURR?\r\n");
            return double.Parse(ret);
        }

        /// <summary>
        /// 读取当前通道
        /// </summary>
        /// <returns></returns>
        public int GetChannel()
        {
            //INSTrument:NSELect?
            var ret = SendAsciiStringAndWaitResponse("INSTrument:NSELect?\r\n");
            return int.Parse(ret);
        }

        /// <summary>
        /// 设置当前通道
        /// </summary>
        /// <param name="channel"></param>
        public void SetChannel(int channel)
        {
            //INSTrument:NSELect?
            SendAsciiString($"INSTrument:NSELect {channel}\r\n");
            Thread.Sleep(_cmdRespTime);
        }

        /// <summary>
        /// 获取识别码
        /// </summary>
        /// <returns></returns>
        public string GetIDN()
        {
            var ret = SendAsciiStringAndWaitResponse("*IDN?\r\n");

            return ret;
        }

        /// <summary>
        /// 必须设置远程模式才可以发送控制指令
        /// </summary>
        public void SetRemote()
        {
            SendAsciiString("SYST:REM\r\n");
        }
    }
}